package com.lsk.servicePassengerUser.service;


import com.lsk.internalcommon.DTO.PassengerUser;
import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.CommonStatusEnum;
import com.lsk.servicePassengerUser.mapper.PassengerUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private PassengerUserMapper passengerUserMapper;

    public ResponseResult loginOrRegister(String passengerPhone){
        System.out.println("service loginOrRegister");
        //根据手机号判断是否有用户
        Map<String, Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);
        List<PassengerUser> list = passengerUserMapper.selectByMap(map);
        System.out.println(list.size()==0?"无记录":list.get(0).getPassengerName());
        //没有用户就添加用户

        if (list.size()==0){
            PassengerUser passengerUser = new PassengerUser();
            passengerUser.setPassengerPhone(passengerPhone);
            passengerUser.setPassengerName("张三");
            passengerUser.setPassengerGender((byte) 0);
            passengerUser.setState((byte) 0);
            LocalDateTime now = LocalDateTime.now();
            passengerUser.setGmtCreate(now);
            passengerUser.setGmtModified(now);

            passengerUserMapper.insert(passengerUser);
        }

        //

        return ResponseResult.success();
    }

    /**
     * 根据手机号查找用户
     * @param passengerPhone
     * @return
     */
    public ResponseResult getUserByPhone(String passengerPhone){
        //根据手机号判断是否有用户
        Map<String, Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);
        List<PassengerUser> list = passengerUserMapper.selectByMap(map);

        if (list.size()==0){
            return ResponseResult.fail(CommonStatusEnum.PASSENGER_NOT_EXIT.getCode(),CommonStatusEnum.PASSENGER_NOT_EXIT.getValue());
        }else {
            PassengerUser p =list.get(0);
            return ResponseResult.success(p);
        }
    }
}
